﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RedBlueBalls
{
    class RedByBlue
    {
        private static bool analyzed = false;
        private static bool checkBest = false;
        private static bool last1;
        private static bool last2;

        public static void analyze(RedBlueBallRecord[] records)
        {
            int size = records.Length;

            if (checkBest)
            {
                int count = 0;
                int count2 = 0;
                for (int i = 1; i < size; i++)
                {
                    if (records[i].HasRedBall(records[i - 1].GetBlue()))
                        count++;
                    if (records[i].HasRedBall(records[i - 1].GetBlue() * 2))
                        count++;
                }

                Tracer.AppendMessage(String.Format("Same Blue {0} / {1}", count, size - 1)); // about 1/3
                Tracer.AppendMessage(String.Format("Double Blue {0} / {1}", count2, size - 1)); // 0%
            }

            last1 = records[size - 1].HasRedBall(records[size - 2].GetBlue());
            last2 = records[size - 2].HasRedBall(records[size - 3].GetBlue());

            analyzed = true;
        }

        public static bool predict(RedBlueBallRecord candidate, RedBlueBallRecord last)
        {
            if (!analyzed)
            {
                Tracer.AppendMessage("Please analyze the data first!");
                return false;
            }

            if (candidate.HasRedBall(last.GetBlue() * 2))
                return false;

            if (last1 || last2)
            {
                // Should not cantain last blue ball
                if (candidate.HasRedBall(last.GetBlue()))
                    return false;
            }
            else
            {
                // Should contain last blue ball
                if (!candidate.HasRedBall(last.GetBlue()))
                    return false;
            }   

            return true;
        }
    }
}
